mini.parse();

var editorId = "briContent";

function CommentsFormHandler() {
}

CommentsFormHandler.instance = new CommentsFormHandler();
CommentsFormHandler.commentsForm = new mini.Form("commentsForm");
CommentsFormHandler.formData = {};

try {
	var editor = KindEditor.create('#' + editorId, {
		resizeType : 1,
		uploadJson : "../../resource/kindeditor/jsp/upload_json.jsp?imgDir="
				+ __msgpath,
		fileManagerJson : '../../resource/kindeditor/jsp/file_manager_json.jsp',
		allowPreviewEmoticons : false,
		allowImageUpload : true,
		items : ['fontname', 'fontsize', '|', 'forecolor', 'hilitecolor',
				'bold', 'italic', 'underline', 'removeformat', '|',
				'justifyleft', 'justifycenter', 'justifyright',
				'insertorderedlist', 'insertunorderedlist', '|', 'emoticons',
				'image', 'link']
	});
} catch (e) {
}

/**
 * 传递数据接口
 * 
 * @param {}
 *            data
 */
CommentsFormHandler.passData = function(data) {
	// 通过页面直接获取数据
	CommentsFormHandler.commentsForm.setData(data);
	document.getElementById("bucTitle").innerHTML = data.buc_title + "的评论";
	document.getElementById("bucName").innerHTML = data.buc_name;
	document.getElementById("bucTime").innerHTML = changeDate(data.buc_time, 6);
	document.getElementById("bucRank").innerHTML = data.buc_rank;
	document.getElementById("bucIp").innerHTML = data.buc_ip;
	CommentsFormHandler.findCommentsReply(data.buc_id);
}

CommentsFormHandler.findCommentsReply = function(id) {
	$.ajax({
				url : __root + "/findCommentsReply.do",
				type : "POST",
				data : {
					briTarget : id
				},
				cache : false,
				success : function(text) {
					if (text.briId != null && text.briId != "") {
						$("#role").text("管理员");
						$("#name").text(text.briName);
						$("#time").text("回复于" + text.briTime);
						$("#content").text("回复内容：" + text.briContent);
						mini.get("briTarget").setValue(text.briTarget);
						mini.get("briId").setValue(text.briId);
					} else {
						mini.get("briTarget").setValue(id);
						$("#content").text("暂无回复");
					}
				}
			});
}

CommentsFormHandler.isReply = false;
CommentsFormHandler.show = function() {
	if (CommentsFormHandler.isReply) {
		CommentsFormHandler.isReply = false;
		mini.get("replyShow").setText("回复");
		$("#replyInfo").show();
		$("#reply").hide();
	} else {
		CommentsFormHandler.isReply = true;
		mini.get("replyShow").setText("收起回复");
		$("#replyInfo").hide();
		$("#reply").show();
	}
	// $("#reply").slideToggle(500);
}

CommentsFormHandler.replyComments = function() {
	// form表单不能嵌套，已经通过form表单加载了，只有通过单个获得数据组装
	var briName = mini.get("briName").getValue();
	var briId = mini.get("briId").getValue();
	var briType = mini.get("briType").getValue();
	var briEmail = mini.get("briEmail").getValue();
	var briTarget = mini.get("briTarget").getValue();
	var briContent = editor.html();
	
	var CommentsData = {
		briName : briName,
		briId : briId,
		briType : briType,
		briEmail : briEmail,
		briTarget : briTarget,
		briContent : briContent
	};
	
	if (briContent.trim() == "") {
		document.getElementById("checkReply").innerHTML = "回复不能为空";
		return;
	} else {
		document.getElementById("checkReply").innerHTML = "";
	}
	
	var json = mini.encode(CommentsData);
	
	$.ajax({
				url : __rootUrl + "saveOrUpdateComments.do",
				type : "POST",
				data : {
					CommentsInfo : json,
					lslUser: gbOperateUser,
    				lslAddress: gbOperateIp
				},
				cache : false,
				success : function(text) {
					mini.alert("回复成功", "系统提示", null);
					CommentsFormHandler.findCommentsReply(briTarget);
					CommentsFormHandler.isReply = false;
					
					mini.get("replyShow").setText("回复");
					$("#replyInfo").show();
					$("#reply").hide();
				},
				error : function(jqXHR, textStatus, errorThrown) {
					mini.alert(jqXHR.responseText, "系统提示", null);
					CommentsFormHandler.closeCurrentWin();
				}
			});
}

CommentsFormHandler.closeCurrentWin = function(action) {
	if (window.CloseOwnerWindow)
		window.CloseOwnerWindow(action);
	else
		window.close();
}

CommentsFormHandler.closeMiniWin = function() {
	CommentsFormHandler.closeCurrentWin("cancel");
};
